# -*- coding: utf-8 -*-
import os
import json

from colorama import init, Fore, Back, Style


class Log():
    def __init__(self):
        self.BRIGHT = Style.BRIGHT
        self.Error_ = Fore.RED + self.BRIGHT
        self.Waring_ = Fore.YELLOW + self.BRIGHT
        self.Info_ = Fore.BLUE + self.BRIGHT
        self.debug_ = Fore.GREEN + self.BRIGHT
        self.other_ = Fore.WHITE + self.BRIGHT
        self.end_ = Fore.RESET + Style.RESET_ALL
        self.if_print = True
        self.preffix = ""
        self.subffix = ""
        self.split_str = "\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n"
        self.use_interface_display_flag = False

    def Error(self,*str__):
        start = self.Error_
        stop  = self.end_

        self.common(start,stop,str__)



    def Waring(self,*str__):
        start = self.Waring_
        stop = self.end_

        self.common(start,stop,str__)

    def Info(self,*str__):
        start = self.Info_
        stop = self.end_

        self.common(start,stop,str__)

    def Debug(self,*str__):
        start = self.debug_
        stop = self.end_

        self.common(start,stop,str__)


    def Other(self,*str__):
        start = self.other_
        stop = self.end_

        self.common(start,stop,str__)

    def set_if_print(self,if_print):
        self.if_print = if_print

    def set_split(self,split_str):
        self.split_str = split_str

    def use_split(self , log_level = "debug"):
        if "debug" in log_level:
            self.Debug(self.split_str)
        if "warning" in log_level:
            self.Waring(self.split_str)
        if "error" in log_level:
            self.Error(self.split_str)


    def add_preffix(self,preffix):
        self.preffix = preffix

    def remove_preffix(self):
        self.preffix = ""

    def add_subffix(self,subffix):
        self.subffix = subffix

    def remove_subffix(self):
        self.subffix = ""

    def use_interface_display(self, length = 0):
        self.use_interface_display_flag = True
        if length == 0:
            self.length = len(self.split_str)
        else:
            self.length = length

    def convert_interface_display(self,str_=""):
        str_list = str_.split("\n")
        out_str = ""
        for i in str_list:
            if len(i) <= self.length-4:
                index = 0
                while index < self.length-4 - len(i):
                    i += "x"
                    index += 1
                i = "x " + i + " x"
                # print("i = ", i)
            out_str += i + "\n"
        return out_str




    def common(self,start,stop,str__):
        if self.if_print == False:
            return
        str_ = start + self.preffix
        for i in str__:
            map_ = {}
            if isinstance(i, list) == True:
                
                str_json = json.dumps(i, indent=2)
                str_ += str_json
            elif isinstance(i, dict) == True:
                map_ = i
                str_json = json.dumps(map_, indent=2)
                str_ += str_json
            else:
                str_ = str_ + str(i)
            if self.split_str not in str_:
                str_ = str_.replace("\n", "%s\n%s" % (self.subffix,self.preffix))

        str_ = str_ + self.preffix + stop
        if self.use_interface_display_flag == True:
            str_ = self.convert_interface_display(str_)
        self.print(str_)


    def print(self,str):
        print(str)



globalLog = Log()

def log_test():
    globalLog.use_split()
    # globalLog.use_interface_display()
    globalLog.add_preffix("\t\t")
    globalLog.Debug("debug")
    globalLog.Error("error")
    globalLog.Info("info")
    globalLog.Other("other")
    globalLog.Debug("list=[1,2,3,4]:", [1, 2, 3, 4,{"1":[1,2,3],"2":2,"3":"3","4":4}])
    globalLog.Debug('map={"1":1,"2":2,"3":"3","4":4}:', {"1": 1, "2": 2, "3": "3", "4": 4})
    globalLog.use_split()
    globalLog.remove_preffix()

if __name__ == "__main__":
    log_test()
